﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XChipTool
{
    public partial class Version : Form
    {
        public System.IO.Ports.SerialPort sPortTemp;
        public Version()
        {
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string filePath = Application.StartupPath+@"\DB\更新记录.txt";
            System.Diagnostics.Process.Start("notepad.exe", filePath);
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //USB是2000f28 
            int aaa = Convert.ToInt32(addr.Text,16);
            List<byte> hexData = new List<byte>();
            for (int i = aaa; i < aaa + 40; i++)
            {
                hexData.Add((byte)SerialPortHelperLib.SerialSend.ReadRegister((ulong)i, sPortTemp));
            }
            string asciiString = Encoding.ASCII.GetString(hexData.ToArray());
            DevExpress.XtraEditors.XtraMessageBox.Show("当前固件版本:" + asciiString);
        }

        private void simpleButton17_Click(object sender, EventArgs e)
        {
            AutoUpdatorHelper.Start("http://49.232.124.138/XChipTool/AutoStarter.xml");
        }
        private DevExpress.Utils.WaitDialogForm WaitBeforeLogin = null;
        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                if (!sPortTemp.IsOpen)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                    return;
                }
                Task.Run(() =>
                {
                    WaitBeforeLogin = new DevExpress.Utils.WaitDialogForm("请稍候...", "正在获取bin名称");
                    WaitBeforeLogin.StartPosition = FormStartPosition.CenterParent;
                    Application.Run(WaitBeforeLogin);
                });
                //SPI使能
                SerialPortHelperLib.SerialSend.WriteRegister(0x805000F1, 0x55, sPortTemp);
                Thread.Sleep(10);
                //获取是否打开bin名称功能
                var isBinName = FlashRead(Convert.ToUInt32("400", 16));
                //获取bin名称长度
                var binlength = FlashRead(Convert.ToUInt32("404", 16));
                if (Convert.ToUInt32(binlength, 16) != 0 && binlength != "FFFFFFFF" && isBinName == "12345678")
                {
                    string str = null;
                    string BinStr = null;
                    for (int i = 0; i < Convert.ToUInt32(binlength, 16) / 2; i += 4)
                    {
                        str = FlashRead((uint)(0x408 + i));
                        BinStr += str;
                    }
                    var binleng = Convert.ToUInt32(binlength, 16) / 2 % 4;
                    if (binleng != 0)
                    {
                        BinStr.Remove((int)(BinStr.Length - binleng * 2));
                    }
                    byte[] bytes = new byte[BinStr.Length / 2];
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        bytes[i] = Convert.ToByte(BinStr.Substring(i * 2, 2), 16);
                    }
                    string aaa = Encoding.UTF8.GetString(bytes);
                    DevExpress.XtraEditors.XtraMessageBox.Show("当前bin名称为:" + aaa);
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("请你检查烧录文件，未找到bin名称");
                }
            }
            catch (Exception)
            {

                throw;
            }
            finally { WaitBeforeLogin.Invoke((EventHandler)delegate { WaitBeforeLogin.Close(); }); }


        }
        private string FlashRead(uint flash_address)
        {
            Thread.Sleep(10);
            long test_value1 = (long)BaseRegisterMap.SPI_BASE_ADDR;

            uint test_value2 = (uint)SPIRegisterMap.SPI_FUNCTION_SELECT;
            uint FUNCT_SEL_ADDR = (uint)(test_value1 + test_value2);

            test_value2 = (uint)SPIRegisterMap.SPI_CONTROL;
            uint CONTROL_ADDR = (uint)(test_value1 + test_value2);

            test_value2 = (uint)SPIRegisterMap.SPI_SHIFT_CONTROL;
            uint SHIFT_ADDR = (uint)(test_value1 + test_value2);

            test_value2 = (uint)SPIRegisterMap.SPI_SHIFT_TXD0;
            uint TXD0_ADDR = (uint)(test_value1 + test_value2);

            test_value2 = (uint)SPIRegisterMap.SPI_SHIFT_TXD1;
            uint TXD1_ADDR = (uint)(test_value1 + test_value2);


            test_value2 = (uint)SPIRegisterMap.SPI_SHIFT_RXD0;
            uint RXD0_ADDR = (uint)(test_value1 + test_value2);

            test_value2 = (uint)SPIRegisterMap.SPI_SHIFT_RXD1;
            uint RXD1_ADDR = (uint)(test_value1 + test_value2);


            //uint flash_address = Convert.ToUInt32(txtBoxFlashAddress.Text, 16);

            uint spi_shift_cnt;
            uint spi_rxvld_cnt;
            uint senddata;
            uint recvdata = 0;
            uint op_code = 0x03;

            FlashBase fb = new FlashBase(sPortTemp);
            //8200000 写 1 
            fb.SendRegDataARM(0x1, FUNCT_SEL_ADDR, 0x1, sPortTemp);
            Thread.Sleep(10);
            //读取的地址 + 3
            senddata = (((flash_address & 0xff) << 24) + ((flash_address & 0xff00) << 8) + ((flash_address & 0xff0000) >> 8) + op_code);
            //82000010 写 读取的地址 + 3
            fb.SendRegDataARM(0x1, TXD0_ADDR, senddata, sPortTemp);
            Thread.Sleep(10);
            spi_shift_cnt = 64;
            spi_rxvld_cnt = 32;
            senddata = (3 << 16) + (spi_rxvld_cnt << 8) + (spi_shift_cnt << 0);
            //8200000c 写 00032040
            fb.SendRegDataARM(0x1, SHIFT_ADDR, senddata, sPortTemp);
            Thread.Sleep(10);
            // 32位读 82000020 
            fb.ReadRegDataARM(0x11, RXD0_ADDR, ref recvdata, sPortTemp);
            var str = Convert.ToString(recvdata, 16).ToUpper();
            var data = (Convert.ToUInt32(str,16) & 0xFF).ToString("X2") + (Convert.ToUInt32(str, 16) >> 8 & 0xFF).ToString("X2") + (Convert.ToUInt32(str, 16) >> 16 & 0xFF).ToString("X2") + (Convert.ToUInt32(str, 16) >> 24 & 0xFF).ToString("X2");
            return data;

        }
    }
}
